package com.gp.learn.common.core.mybaitsplus.conditions;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gp.learn.common.core.mybaitsplus.JoinBean;
import com.gp.learn.common.core.mybaitsplus.annotation.TableAlias;
import com.gp.learn.common.core.utils.CommonUtils;
import com.gp.learn.common.core.utils.ReflectionUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 *
 * @param <T>
 */
public class SuperQueryWrapper<T> extends QueryWrapper<T> {

    /**
     * 表名称
     */
    private String tableName;

    /**
     * 表别名
     */
    private String tableAlias;

    /**
     * 查询字段
     */
    private HashSet<String> select = new HashSet<>();

    /**
     * 连接查询的表
     */
    private List<JoinBean> joinSqList = new ArrayList<>();

    /**
     * 连接查询
     */
    private String joinSql;

    private Class<T> cls;

    public SuperQueryWrapper() {

    }

    public SuperQueryWrapper(Class<T> cls) {
        this.cls = cls;
        //利用反射
        String tableName = ReflectionUtils.getTableName(cls);
        this.tableName = tableName;
        String tableAlias = ReflectionUtils.getTableAlias(cls);
        this.tableAlias = tableAlias;

    }

    public SuperQueryWrapper select(String... column) {
        for (String filed : column) {
            this.select.add(filed);
        }
        return this;
    }

    public SuperQueryWrapper select(Class cls, String... field) {
        TableAlias tableAlias = AnnotationUtils.getAnnotation(cls, TableAlias.class);
        String alias = tableAlias.value();
        for (String s : field) {
            StringBuffer stringBuilder1 = new StringBuffer(s);
            if (s.indexOf("(") > -1) {
                stringBuilder1.insert(s.indexOf("(") + 1, alias + ".");
            } else {
                stringBuilder1.insert(0, alias + ".");
            }
            this.select.add(stringBuilder1.toString());
        }
        return this;
    }

    public SuperQueryWrapper addSelect(String... column) {
        for (String filed : column) {
            this.select.add(filed);
        }
        return this;
    }

    public SuperQueryWrapper addSelect(Class<T> cls, String... column) {
        String tableAlias = ReflectionUtils.getTableAlias(cls);
        for (String filed : column) {
            this.select.add(tableAlias + "." + filed);
        }
        return this;
    }

    public String getTableName() {
        return tableName;
    }

    public String getTableAlias() {
        return tableAlias;
    }

    //获取查询字段
    public String getSelect() {
        //先获取原生的查询字段
        String sqlSelect = getSqlSelect();
        if (CommonUtils.isNotEmpty(sqlSelect)) {
            select.add(String.join(",", sqlSelect));
        }
        if (CollectionUtils.isEmpty(select)) {
            select.add("*");
        }
        return String.join(",", select);
    }

    //获取连接查询sql
    public String getJoinSql() {
        StringBuilder builder = new StringBuilder();
        String sql = "%s JOIN %s %s ON %s.%s= %s.%s\n";
        if (!ObjectUtils.isEmpty(joinSqList)) {
            joinSqList.forEach(item -> builder.append(String.format(sql, item.getJoinType().name(), item.getJoinTableName(), item.getJoinTableAlias(), item.getMainTableAlias(), item.getMainField(), item.getJoinTableAlias(), item.getJoinField())));
        }
        return builder.toString();
    }

    public JoinBean createJoin(Class joinClass) {
        JoinBean joinBean = new JoinBean(this.cls, joinClass);
        this.joinSqList.add(joinBean);
        return joinBean;
    }

}
